/**
 * @author Holger Storm, kzm
 * @version 1.0
 * @license MIT-License (2007)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

 */
package com.mediacatalyst.core {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	import com.mediacatalyst.lang.IDeconstructable;
	import com.mediacatalyst.lang.IPrintable;
	import com.mediacatalyst.net.LinkedLoader;
	import com.mediacatalyst.net.LinkedLoaderList;	

	/**
	 * Loader que for mediacatalyst-style loading of asstes and properties.
 */
	public class ContentControler implements IDeconstructable, IPrintable{ 
		private static var _instance:ContentControler;
		private var _chain:LinkedLoaderList;
		private var _current:LinkedLoader;
		private var _inprogress:Boolean;

		/**
		 * @param makeSure Prevents creation of instances.
		 */
		public function ContentControler(caller:Function=null ){
			if( caller == null ||  caller != ContentControler.getInstance )
				throw new Error ("ContentControler is a singleton class, use Singleton.getInstance() instead");
			_chain = new LinkedLoaderList();
			_inprogress = false;
		}
		/**
		 * @see IDeconstructable#deconstruct()
		 */
		public function deconstruct():void{
			_chain = null;
			_current = null;
		}
		/**
		 * Singleton access method.
		 */
		public static function getInstance():ContentControler{
			if(_instance == null){
				_instance = new ContentControler(arguments['callee']);
			}
			return _instance;
		}
		/**
		 * Returns the loader chain.
		 */
		public function get chain():LinkedLoaderList{
			return _chain;
		}
		/**
		 * Starts the load process.
		 */
		public function start():void{
			if(_current==null)
				reset();
			_inprogress = true;
			_current.addEventListener(Event.COMPLETE,_next);
			_current.addEventListener(IOErrorEvent.IO_ERROR, _next);
			_current.loadURL();
		}
		/**
		 * Stops the load process.
		 */
		public function stop():void{
			_inprogress = false;
			_current.close();
		}
		/**
		 * Resets the loading process.
		 */
		public function reset():void{
			_current = LinkedLoader(_chain.head);
		}
		private function _next(e:Event):void{
			_current.addEventListener(Event.COMPLETE,_next);
			_current.removeEventListener(IOErrorEvent.IO_ERROR, _next);
			if(!_current.hasNext() || !_inprogress)
				return;
			_current = LinkedLoader(_current.next);
			_current.addEventListener(Event.COMPLETE,_next);
			_current.addEventListener(IOErrorEvent.IO_ERROR, _next);
			_current.loadURL();
		}
		/**
		 * @see com.mediacatalyst.lang.IPrintable#toString()
		 */
		public function toString():String{
			return "ContentControler(chain="+chain+",current="+_current+",progress="+_inprogress+")";
		}
	}
}